package com.example.netchat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class ContentUtils {
	public static class Room {
		public long _id;
		public String room_id;
		public String name;
		public boolean favorite;
		public String last_reg_id;
	}

	public static Room getRoomById(Context context, long id) {
		Cursor c = context
				.getContentResolver()
				.query(RoomsContentProvider.CONTENT_URI,
						RoomsContentProvider.ALL_COLUMNS,
						RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ID_COLUMN_INDEX]
								+ "=?", new String[] { Long.toString(id) },
						null);
		try {
			if (c.moveToNext()) {
				return parseCursor(c);
			} else {
				return null;
			}
		} finally {
			c.close();
		}

	}

	public static Room getRoomByRoomId(Context context, String roomId) {
		Cursor c = context
				.getContentResolver()
				.query(RoomsContentProvider.CONTENT_URI,
						RoomsContentProvider.ALL_COLUMNS,
						RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ROOM_ID_COLUMN_INDEX]
								+ "=?", new String[] { roomId }, null);
		try {
			if (c.moveToNext()) {
				return parseCursor(c);
			} else {
				return null;
			}
		} finally {
			c.close();
		}

	}

	private static Room parseCursor(Cursor c) {
		Room room = new Room();
		room._id = c.getLong(RoomsContentProvider.ID_COLUMN_INDEX);
		room.name = c.getString(RoomsContentProvider.ROOM_NAME_COLUMN_INDEX);
		room.room_id = c.getString(RoomsContentProvider.ROOM_ID_COLUMN_INDEX);
		if (!c.isNull(RoomsContentProvider.LAST_REG_ID_COLUMN_INDEX)) {
			room.last_reg_id = c
					.getString(RoomsContentProvider.LAST_REG_ID_COLUMN_INDEX);
		}

		if (!c.isNull(RoomsContentProvider.FAVORITE_COLUMN_INDEX)) {
			room.favorite = c
					.getLong(RoomsContentProvider.FAVORITE_COLUMN_INDEX) == 1;
		}

		return room;
	}

	public static void updateRoom(Context context, Room room) {
		ContentValues values = new ContentValues();
		values.put(
				RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ROOM_ID_COLUMN_INDEX],
				room.room_id);
		values.put(
				RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ROOM_NAME_COLUMN_INDEX],
				room.name);

		if (room.last_reg_id != null) {
			values.put(
					RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.LAST_REG_ID_COLUMN_INDEX],
					room.last_reg_id);
		}
		if (room.favorite) {
			values.put(
					RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.FAVORITE_COLUMN_INDEX],
					1);
		} else {
			values.put(
					RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.FAVORITE_COLUMN_INDEX],
					0);
		}

		if (room._id == 0) {
			context.getContentResolver().insert(
					RoomsContentProvider.CONTENT_URI, values);
		} else {
			context.getContentResolver()
					.update(RoomsContentProvider.CONTENT_URI,
							values,
							RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ID_COLUMN_INDEX]
									+ "=?",
							new String[] { Long.toString(room._id) });
		}
	}

	public static List<String> getAllRoomIds(Context context) {
		List<String> roomIds = new ArrayList<String>();

		Cursor c = context
				.getContentResolver()
				.query(RoomsContentProvider.CONTENT_URI,
						new String[] { RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ROOM_ID_COLUMN_INDEX] },
						null, new String[0], null);

		try {
			while (c.moveToNext()) {
				roomIds.add(c.getString(0));
			}
		} finally {
			c.close();
		}

		return roomIds;
	}

	public static List<String> getAllRoomIdsWithDifferentLastRegId(
			Context context, String regId) {
		List<String> roomIds = new ArrayList<String>();

		Cursor c = context
				.getContentResolver()
				.query(RoomsContentProvider.CONTENT_URI,
						new String[] { RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ROOM_ID_COLUMN_INDEX] },
						RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.LAST_REG_ID_COLUMN_INDEX]
								+ "!=?", new String[] { regId }, null);

		try {
			while (c.moveToNext()) {
				roomIds.add(c.getString(0));
			}
		} finally {
			c.close();
		}

		return roomIds;
	}

	public static void deleteRoom(Context context, long id) {
		context.getContentResolver()
				.delete(RoomsContentProvider.CONTENT_URI,
						RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.ID_COLUMN_INDEX]
								+ "=?", new String[] { Long.toString(id) });
	}

	public static void saveMessage(Context context, String roomId, String from,
			String message, Date date) {
		ContentValues values = new ContentValues();

		values.put(
				MessagesContentProvider.ALL_COLUMNS[MessagesContentProvider.ROOM_ID_COLUMN_INDEX],
				roomId);
		values.put(
				MessagesContentProvider.ALL_COLUMNS[MessagesContentProvider.MESSAGE_COLUMN_INDEX],
				message);
		values.put(
				MessagesContentProvider.ALL_COLUMNS[MessagesContentProvider.FROM_COLUMN_INDEX],
				from);
		values.put(
				MessagesContentProvider.ALL_COLUMNS[MessagesContentProvider.DATETIME_COLUMN_INDEX],
				date.getTime());

		context.getContentResolver().insert(
				MessagesContentProvider.CONTENT_URI, values);
	}
}
